package cn.minhx.types.design.tree;

public abstract class AbstractMultiThreadStrategyRouter<T, D, R>
		implements
			StrategyMapper<T, D, R>,
			StrategyHandler<T, D, R> {

	protected StrategyHandler<T, D, R> defaultStrategyHandler = StrategyHandler.DEFAULT;

	public R router(T requestParameter, D dynamicContext) throws Exception {
		StrategyHandler<T, D, R> strategyHandler = next(requestParameter, dynamicContext);
		if (null != strategyHandler) {
			return strategyHandler.apply(requestParameter, dynamicContext);
		}
		return defaultStrategyHandler.apply(requestParameter, dynamicContext);
	}

	@Override
	public R apply(T requestParameter, D dynamicContext) throws Exception {
		// 异步加载数据
		loadContext(requestParameter, dynamicContext);
		// 业务流程受理
		return doApply(requestParameter, dynamicContext);
	}

	/** 异步加载数据 */
	protected abstract void loadContext(T requestParameter, D dynamicContext) throws Exception;

	/** 业务流程受理 */
	protected abstract R doApply(T requestParameter, D dynamicContext) throws Exception;
}
